package com.cookbook.animals;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.database.sqlite.*;
import java.util.*;


public class History extends Activity {
	// Declare variable to hold database helper and database
	DatabaseHelper db_helper;
	SQLiteDatabase db;
	
	// Declare variable to hold percent
	double percent;
	
	// Declare variables to hold text views
	TextView stats_display;
	TextView problem1;
	TextView problem2;
	TextView problem3;
	
	// Declare String array to hold names of animals
	ArrayList<String> names = new ArrayList<String>();
	
	// Declare a variable to hold current user name
	String user;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    	// Instantiate new DatabaseHelper variable
    	db_helper = new DatabaseHelper(this);
    
    	// Open database for reading and writing
    	db = db_helper.getWritableDatabase();
    	
        // Set layout
        setContentView(R.layout.history);
        
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so get user name from this bundle
        	user = savedInstanceState.getString("user");
        }
        
        // Else get user name from parent activity
        else{
        	user = this.getIntent().getStringExtra("user");
        }
        
        // Instantiate 4 text views to be updated later
        stats_display = (TextView) findViewById(R.id.stat);
        problem1 = (TextView) findViewById(R.id.problem1);
        problem2 = (TextView) findViewById(R.id.problem2);
        problem3 = (TextView) findViewById(R.id.problem3);
        
     // Fill names array with a buffer spot and all of the animals names
        names.add("buffer");
        names.add("Cow");
        names.add("Pig");
        names.add("Dog");
        names.add("Horse");
        names.add("Sheep");
        names.add("Cat");
        names.add("Chicken");
        names.add("Squirrel");
        names.add("Duck");
        names.add("Frog");
        names.add("Turkey");
        names.add("Lion");
        names.add("Tiger");
        names.add("Bear");
        names.add("Monkey");
        names.add("Fish");
        names.add("Parrot");
        names.add("Dolphin");
        names.add("Eagle");
        names.add("Donkey");
        names.add("Wolf");
        names.add("Snake");
        names.add("Mouse");
        names.add("Elephant");
        names.add("Bee");
        names.add("Bat");
        names.add("Cricket");
        names.add("Goose");
        names.add("Rooster");
        names.add("Owl");
        
        // Get stats from database
        ArrayList<Long> stats = db_helper.getStats(db, user);
        
        // Close the database
        db.close();
        
        // Make sure total is not 0
        if(stats.get(1).intValue() != 0){
        	// If so calculate percentage
            percent = (stats.get(0).doubleValue()/stats.get(1).doubleValue()) * 100;
        }

        // Else set percent to be 0
        else{
        	percent = 0;
        }
        
        // Format stats output string
        String out = String.format("%d correct out of %d total. %.3g%% correct", stats.get(0).intValue(), stats.get(1).intValue(), percent);
    
        // Output stats string to stats_display
        stats_display.setText(out);
        
        // Declare four longs to hold numbers for two animals to be compared
        Long numbers1;
        Long numbers2;
        Long numbers3;
        Long numbers4;
        
        // Set three highest frequencies to be the first 3 initially
        int id_1 = 1;
        int id_2 = 2;
        int id_3 = 3;
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Check if frequency of id_1 is bigger than that of id_2
        numbers1 = db_helper.getFrequency(db, user, id_1);
        numbers2 = db_helper.getFrequency(db, user, id_2);
        if(numbers1.intValue() < numbers2.intValue()){
        	// If not then switch the two
        	int temp = id_1;
        	id_1 = id_2;
        	id_2 = temp;
        }
        
        // Check if frequency of id_1 is bigger than that of id_3
        numbers1 = db_helper.getFrequency(db, user, id_1);
        numbers2 = db_helper.getFrequency(db, user, id_3);
        if(numbers1.intValue() < numbers2.intValue()){
        	// If not then switch the two
        	int temp = id_1;
        	id_1 = id_3;
        	id_3 = temp;
        }
        
        // Check if frequency of id_2 is bigger than that of id_3
        numbers1 = db_helper.getFrequency(db, user, id_2);
        numbers2 = db_helper.getFrequency(db, user, id_3);
        if(numbers1.intValue() < numbers2.intValue()){
        	// If not then switch the two
        	int temp = id_2;
        	id_2 = id_3;
        	id_3 = temp;
        }
        
        // Close the database
        db.close();
        
        // Increment through all entries in database to find 3 highest frequencies
        for(int i = 4; i <= DatabaseHelper.DB_SIZE; i++){
        	// Open the database
        	db = db_helper.getWritableDatabase();
        	
        	// Get the frequencies of this element and the current top 3
        	numbers1 = db_helper.getFrequency(db, user, id_1);
        	numbers2 = db_helper.getFrequency(db, user, id_2);
        	numbers3 = db_helper.getFrequency(db, user, id_3);
        	numbers4 = db_helper.getFrequency(db, user, i);
        	
        	// Close the database
        	db.close();
        	
        	// Check if frequency of current element is higher than the highest
        	if(numbers4.intValue() > numbers1.intValue()){
        		// If so then make current element the highest and filter down
        		id_3 = id_2;
        		id_2 = id_1;
        		id_1 = i;
        	}
        	
        	// Else check if frequency of current element is higher than 2nd highest
        	else if (numbers4.intValue() > numbers2.intValue()){
        		// If so then make current element the 2nd highest and filter down
        		id_3 = id_2;
        		id_2 = i;
        	}
        	
        	// Else check if frequency of current element is higher than 3rd highest
        	else if(numbers4.intValue() > numbers3.intValue()){
        		// If so then make current element the 3rd highest
        		id_3 = i;
        	}
        }
        
        // Open the database
        db = db_helper.getWritableDatabase();
        
        // Get frequencies of final top 3 animals
        numbers1 = db_helper.getFrequency(db, user, id_1);
        numbers2 = db_helper.getFrequency(db, user, id_2);
        numbers3 = db_helper.getFrequency(db, user, id_3);
        
        // Close the database
        db.close();
        
        // Check if frequency of worst animal is more than 1
        if(numbers1.intValue() > 1){
        	// If so output animals name and continue
        	problem1.setText("Worst animal: "+names.get(id_1));
        	
        	// Check if frequency of second worst animal is more than 1
        	if(numbers2.intValue() > 1){
        		// If so output animals name and continue
                problem2.setText("Second worst animal: "+names.get(id_2));
                
                // Check if frequency of third worst animal is more than 1
                if(numbers3.intValue() > 1){
                	// If so output animals name
                    problem3.setText("Third worst animal: "+names.get(id_3));                	
                }
                
                // Else make third worst animal blank
                else{
                	problem3.setText(" ");
                }
        	}
        	
            // Else make second and third worst animals blank
            else{
            	problem2.setText(" ");
            	problem3.setText(" ");
            }
        }
        
        // Else output there are no problem animals
        else{
        	problem1.setText("No Problem animals");
        	problem2.setText(" ");
        	problem3.setText(" ");
        }
        
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put user into outState bundle
    	outState.putString("user", user);
    }
}